package com.example.t470p.facechecktickets.facesdk;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;

import com.synjones.handsetS8.MyUtils;

import java.io.File;
import java.io.FileInputStream;
import java.math.BigDecimal;

import cn.face.localsdkdemo.ConStant;
import cn.face.localsdkdemo.sdkproc.FeatureBean;
import cn.face.localsdkdemo.sdkproc.LocalSDK;
import cn.face.localsdkdemo.sdkproc.VerifyBean;

public class FaceServiceV2 extends Service {
    private static final  String licensePath="synjones/idcard/";
    public static final String FACE_LICENSE_PATH= MyUtils.getExternalPath()+licensePath+"faceLicense";
    private LocalSDK mFaceSDK = LocalSDK.getInstance(null);;
    public FaceServiceV2() {
    }

    private IFaceV2Service.Stub mBinder=new IFaceV2Service.Stub() {
        @Override
        public int init() throws RemoteException {
            return initSDk();
        }

        @Override
        public byte[] getIDCardPhotoFeature(byte[] bytesIDCardPhoto) throws RemoteException {
            FeatureBean mIdcardFiledFeatureBean = mFaceSDK.GetFeatureFromImgData(bytesIDCardPhoto, true);
            if(mIdcardFiledFeatureBean!=null)
                return mIdcardFiledFeatureBean.btFeature;
            return null;
        }

        @Override
        public byte[] getPresentPhotoFeature(byte[] bytesPresentPhoto) throws RemoteException {
            FeatureBean mPresentProbeFeatureBean = mFaceSDK.GetFeatureFromImgData(bytesPresentPhoto, false);
            if(mPresentProbeFeatureBean!=null)
                return mPresentProbeFeatureBean.btFeature;
            return null;
        }

        @Override
        public int compareFeature(byte[] idcardPhotoFea, byte[] presentPhotoFea) throws RemoteException {
            VerifyBean mVerifyBean = mFaceSDK.Verify( presentPhotoFea, idcardPhotoFea);
            if (mVerifyBean.ret == 0) {
                BigDecimal decimal = new BigDecimal(mVerifyBean.score);
                BigDecimal decimal2 = new BigDecimal((float) 0.92139107);
                if (!decimal.equals(decimal2))//有时候识别分数是这个值的时候并不是同一人 可能是截取的照片有问题
                    return (int) (mVerifyBean.score*100);
                return -1;
            }
            return 0;
        }

        @Override
        public int deinit() throws RemoteException {
            mFaceSDK.DestoryHandles();
            return 0;
        }
    };

    protected int initSDk() {
        String license=null;
        File f=new File(FACE_LICENSE_PATH);
        if(f.exists()){
            license= new String(readAllSdcardFile(FACE_LICENSE_PATH));
        }
        if(license!=null)
            ConStant.sLicence=license;
        // 创建句柄，句柄只需要创建一次，程序启动创建，退出时再销毁
        int iRet = mFaceSDK.CreateHandles(ConStant.sLicence,
                ConStant.faceMinSize, ConStant.faceMaxSize, ConStant.sModelDir);
        if (iRet != 0) {
            // 创建句柄失败，请根据错误码检测原因
            //Toast.makeText(mContext, "创建句柄失败，错误码: " + iRet, Toast.LENGTH_LONG).show();
           // m_bInit = false;
        } else {
           // m_bInit = true;
        }
        return iRet;
    }

    public static byte [] readAllSdcardFile(String fileName){
        // String res="";
        byte [] buffer = null;

        try{
            FileInputStream fin = new FileInputStream(fileName);
            int length = fin.available();
            buffer = new byte[length];
            fin.read(buffer);
            fin.close();
        }
        catch(Exception e){
            e.printStackTrace();
            return null;
        }
        return buffer;
    }



    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }
}
